<?php

namespace App\Models;

class Helps extends Base
{
    protected $table = 'helps';

    const STATUS_1 = 1;
    const STATUS_2 = 2;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--info">锁定</span>',
    ];

    const TYPE_1 = 1;
    const TYPE_2 = 2;

    const TYPE_TEXT = [
        self::TYPE_1 => '<span class="el-tag">新手指南</span>',
        self::TYPE_2 => '<span class="el-tag  el-tag--danger">平台细则</span>',
    ];

    protected $fillable = [
        'id','title','content','type','status','created_at','updated_at'
    ];

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('title','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['type']) && $params['type']) {
                    $query ->where('type',$params['type']);
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
            })->where('status','<>',-1)
            ->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
            'type'   => self::TYPE_TEXT,
        ]);
        return $lists;
    }

}
